# -*- coding: utf-8 -*-

import time
import requests
from selenium.webdriver import Keys
from public.Base import PageMethod
from public.config import log
from public.httpBase import httpGet


def Login(driver, msmvalue, mobile, tid):
        user = PageMethod(driver)
        try:
                # 输入验证码
                user.click("xpath", "//input[@placeholder='验证码']", tid)
                user.type("xpath", "//input[@placeholder='验证码']", Keys.CONTROL + "a", tid)
                user.type("xpath", "//input[@placeholder='验证码']", Keys.DELETE, tid)

                user.type("xpath", "//input[@placeholder='验证码']", msmvalue, tid)
                # 点击登录
                user.xclick("class", "css-1jgt0wa.css-kyhkf6.dyn", tid)

                start_time = time.time()

                while time.time() - start_time < 5:
                        cookies = driver.get_cookies()
                        logged_in = any(cookie['name'] == 'access-token-creator.xiaohongshu.com' for cookie in cookies)
                        if logged_in:
                                log.info("logged_in为True", tid)
                                user.quit_browser(tid)
                                userInfo = userinfo(cookies, mobile, tid)
                                return cookies, userInfo

                        else:
                                element = user.find_element("class", "css-1qf7tqh", tid)
                                if element:
                                        text = user.get_text(element, tid)
                                        log.info(text, tid)
                                        if text:
                                                if text != "Error":
                                                        return text
                                                elif text == "Error":
                                                        Y = 1
                                                        while Y < 100:
                                                                text = user.get_text(element, tid)
                                                                if text != "Error":
                                                                        break
                                                                Y = Y+1
                                                                time.sleep(0.5)
                                                        log.info(text, tid)
                                                        if text:
                                                                return text
                                                        else:
                                                                continue
                                        else:
                                                continue
                log.info(f"登录超时：{time.time() - start_time}", tid)
                return "登录超时，请重试"
        except BaseException as e :
                user.quit_browser(tid)
                log.error(f"登錄失敗，退出瀏覽器{e}", tid)
                raise


def userinfo(driver_cookies, mobile, tid):

        url = "https://creator.xiaohongshu.com/api/galaxy/user/info"
        session_cookies = requests.cookies.RequestsCookieJar()

        # 将 WebDriver 中获取的 cookies 添加到 requests 的 cookie jar
        for cookie in driver_cookies:
                session_cookies.set(cookie['name'], cookie['value'], domain=cookie.get('domain', ''),
                                    path=cookie.get('path', '/'))

        text = httpGet(url, 'https://creator.xiaohongshu.com/creator/home', session_cookies, mobile, tid)
        log.info(f"info接口返回{text}", tid)
        return text
